﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// RssFeedBuilder 的摘要说明。
    /// </summary>
    public class RssFeedBuilder
    {
        public int CategoryId { get; set; }

        public RssFeedBuilder()
        {
            CategoryId = int.Parse(HttpContext.Current.Request.QueryString["CategoryId"]);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            IList<Core> list = new CoreManage().CategoryPageBindList(CategoryId, null,
                new PagingParam {PageNo = 1, PageSize = 15});


            BuildingXml(list);
        }

        #region  建立xml

        public void BuildingXml(IList<Core> list)
        {
            rss _rss = new rss {Version = "2.0"};
            if (list.Any())
            {
                _rss.channel.title = string.Empty;
            }
            else
            {
                _rss.channel.title = CategoryManage.GetInfo(list[0].CategoryId).CategoryName;
            }
            _rss.channel.description = string.Empty;
            _rss.channel.language = "zh-cn";
            _rss.channel.generator = "WinnerCMS";
            _rss.channel.link = WebConfig.DomainName;
            _rss.channel.copyright = string.Format("Copyright 2005-{0} {1}, All Rights Reserved", DateTime.Now.Year,
                WebConfig.SiteName);
            _rss.channel.pubDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            _rss.channel.Items = new channel.item[list.Count];

            for (int i = 0; i < list.Count; i++)
            {
                channel.item I = new channel.item();
                I.title = list[i].ContentTitle;
                I.link = WebConfig.DomainName + list[i].Link;
                I.author = string.Empty;
                I.description = list[i].Brief;
                I.guid = string.Empty;
                I.category = string.Empty;

                _rss.channel.Items[i] = I;
            }
            HttpContext.Current.Response.ContentType = "text/xml";

            HttpContext.Current.Response.Write(_rss.ToXml());
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class rss
    {
        public rss()
        {
            channel = new channel();
        }

        [XmlAttribute("version")]
        public string Version { get; set; }

        public channel channel { get; set; }
    }

    [Serializable]
    public class channel
    {
        public string title { get; set; }

        public string link { get; set; }

        public string language { get; set; }

        public string generator { get; set; }


        public string copyright { get; set; }

        public string pubDate { get; set; }

        public string description { get; set; }

        [XmlElement("item")]
        public item[] Items { get; set; }

        public class item
        {
            public string title { get; set; }
            public string link { get; set; }

            public string author { get; set; }

            public string guid { get; set; }
            public string category { get; set; }

            public string description { get; set; }
        }
    }
}